package com.bjsxt.userCenter.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil
{
	public String methodGet(String urlStr,Map<String,String> headMap,Map<String,String> paramMap)
	{
		StringBuilder sb = new StringBuilder();
		try
		{
			if(!urlStr.endsWith("?"))
			{
				urlStr += "?";
			}
			for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry entry = (Entry) iterator.next();
				urlStr +=entry.getKey()+"="+entry.getValue()+"&";
			}
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			for (Iterator iterator = headMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry entry = (Entry) iterator.next();
				urlConnection.setRequestProperty(entry.getKey()+"", entry.getValue()+"");
			}
			InputStream is = urlConnection.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
			String line = "";
			while((line = br.readLine()) != null)
			{
				sb.append(line);
			}
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public String methodPost(String urlStr,Map<String,String> headMap,Map<String,String> paramMap)
	{
		StringBuilder sb = new StringBuilder();
		try
		{
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			//开启输入输出
			urlConnection.setDoInput(true);
			urlConnection.setDoOutput(true);
			//设置信封的内容
			for (Iterator iterator = headMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry entry = (Entry) iterator.next();
				urlConnection.setRequestProperty(entry.getKey()+"", entry.getValue()+"");
			}
			StringBuilder paramStr = new StringBuilder();
			for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry entry = (Entry) iterator.next();
				paramStr.append(entry.getKey()+"="+entry.getValue()+"&");
			}
			OutputStream os = urlConnection.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
			bw.write(paramStr.toString());
			bw.flush();
			bw.close();
			InputStream is = urlConnection.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
			String line = "";
			while((line = br.readLine())!= null)
			{
				sb.append(line);
			}
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public void methodLogout(String urlStr,Map<String,String> headMap,Map<String,String> paramMap)
	{
		StringBuilder sb = new StringBuilder();
		try
		{
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			//开启输入输出
			urlConnection.setDoInput(true);
			urlConnection.setDoOutput(true);
			//设置信封的内容
			for (Iterator iterator = headMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry entry = (Entry) iterator.next();
				urlConnection.setRequestProperty(entry.getKey()+"", entry.getValue()+"");
			}
			StringBuilder paramStr = new StringBuilder();
			for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry entry = (Entry) iterator.next();
				paramStr.append(entry.getKey()+"="+entry.getValue()+"&");
			}
			OutputStream os = urlConnection.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
			bw.write(paramStr.toString());
			bw.flush();
			bw.close();
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
